﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Layout.Pattern;
using System.IO;
using log4net.Core;
using System.Reflection;

namespace BPMS.Utility.Logging
{

    public class CustomPatternLayoutConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            if (Option != null)
            {
                WriteObject(writer, loggingEvent.Repository, GetPropertyValue(Option, loggingEvent));
            }
            else
            {
                WriteDictionary(writer, loggingEvent.Repository, loggingEvent.GetProperties());
            }
        }

        private object GetPropertyValue(string propertyName, LoggingEvent loggingEvent)
        {
            PropertyInfo property = loggingEvent.MessageObject.GetType().GetProperty(propertyName);
            if (null != property)
            {
                return property.GetValue(loggingEvent.MessageObject, null);
            }
            return string.Empty;
        }
    }
}
